package org.openurp.edu.student.info.web.action

import org.beangle.webmvc.entity.action.RestfulAction
import org.openurp.edu.student.info.model.Schooling
import org.openurp.edu.student.info.model.Semester
import org.openurp.edu.student.info.web.helper.ExportExcelUtils
import org.beangle.data.dao.OqlBuilder
import org.beangle.commons.lang.ClassLoaders
import java.io.OutputStream
import org.beangle.webmvc.api.view.View
import org.beangle.webmvc.api.action.ServletSupport
import org.beangle.webmvc.api.annotation.response

/**
 * @author xinzhou
 */

/**
 * 其他教师查看收费信息
 */
class PaymentTeacherAction extends RestfulAction[Schooling]  with ServletSupport {

  override protected def indexSetting(): Unit = {
    put("semesters", entityDao.getAll(classOf[Semester]).sortBy { semester => semester.id })
    val semesterQuery = OqlBuilder.from(classOf[Semester], "semester").where(":now between semester.beginOn and semester.endOn", new java.util.Date())
    put("currentSemester", entityDao.search(semesterQuery).head)
  }

  override def search(): String = {
    put("math", new Math3)
    super.search()
  }

  def exportpayment(): View = {
    val list = collection.JavaConversions.asJavaCollection(entityDao.search(this.getQueryBuilder()))
    //查出学生信息并放到map中
    val beans = new java.util.HashMap[String, Any]
    beans.put("list", list);
    //获得模板路径
    val path = ClassLoaders.getResourceAsStream("student_payment.xls");
    //准备输出流
    var os: OutputStream = null;
    response.setContentType("application/x-excel");
    response.setHeader("Content-Disposition", "attachment;filename=studentPay.xls");
    os = response.getOutputStream();
    ExportExcelUtils.excprtStudentExcel(beans, path, os);
    null
  }
}

class Math3 {
  def parse(s: String): Double = {
    if (null == s) 0d
    else java.lang.Double.parseDouble(s)
  }
}